﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using clBUS;
using clEntidad;
using clEntidad.Ex;

namespace portero_.Admin
{
    public partial class frmMttoLocal : Form
    {
        public frmMttoLocal()
        {
            InitializeComponent();
        }

        private void tsbSalir_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void frmMttoLocal_Load(object sender, EventArgs e)
        {
            cargar();
        }
        private void select(int id)
        {
            foreach (DataGridViewRow row in dgvLocal.Rows)
            {
                int idRow = (int)row.Cells["id"].Value;
                if (idRow == id)
                {
                    row.Selected = true;
                    return;
                }
            }
        }

        private void cargar()
        {
            BUSLocal objBUS = new BUSLocal(Constantes.CADENA_BD);
            dgvLocal.Rows.Clear();
            foreach(Local elem in objBUS.getAll())
            {
                int numRow = dgvLocal.Rows.Add();
                dgvLocal.Rows[numRow].Cells["id"].Value = elem.id;
                dgvLocal.Rows[numRow].Cells["nombre"].Value = elem.nombre;
            }
        }

        private void tsbAdd_Click(object sender, EventArgs e)
        {
            int id=0;
            if (dgvLocal.SelectedRows.Count == 1)
            {
                id = (int)dgvLocal.SelectedRows[0].Cells["id"].Value;
            }
            frmLocal ofrmLoc = new frmLocal(null);
            ofrmLoc.ShowDialog();
            cargar();
            select(id);
        }

        private void tsbEdit_Click(object sender, EventArgs e)
        {
            if (dgvLocal.SelectedRows.Count == 1)
            {
                int id = (int)dgvLocal.SelectedRows[0].Cells["id"].Value;
                Local local = new Local();
                local.id=id;
                frmLocal ofrmLocal = new frmLocal(local);
                ofrmLocal.ShowDialog();
                cargar();
                select(id);
            }
        }

        private void tsbDel_Click(object sender, EventArgs e)
        {
            if (dgvLocal.SelectedRows.Count == 1)
            {
                int id = (int)dgvLocal.SelectedRows[0].Cells["id"].Value;
                Local local = new Local();
                local.id = id;
                local.nombre = (String)dgvLocal.SelectedRows[0].Cells["nombre"].Value;
                if (MessageBox.Show("Desea eliminar " + local.nombre + "?", Constantes.cNOMBRE_SIS, MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
                {
                    BUSLocal objBUS = new BUSLocal(Constantes.CADENA_BD);
                    try
                    {
                        objBUS.del(id);
                        cargar();
                    }
                    catch (BUSException ex)
                    {
                        MessageBox.Show(ex.Message,Constantes.cNOMBRE_SIS,MessageBoxButtons.OK,MessageBoxIcon.Information);
                    }
                }
                
            }



        }
    }
}
